package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Personal;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.repository.PersonalRepository;
import cn.wolfcode.wolf2w.repository.UserInfoRepository;
import cn.wolfcode.wolf2w.service.IPersonalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PersonalServiceImpl implements IPersonalService {
    @Autowired
    PersonalRepository personalRepository;
    @Autowired
    UserInfoRepository userInfoRepository;
    @Override
    public List<UserInfo> getVisitors(String uid) {
        Personal p = personalRepository.findByuserId(uid);
        System.out.println(p);
        List<UserInfo> userInfos = new ArrayList<>();
//        System.out.println(p.getVisitorIds().size());
        for (String vid : p.getVisitorIds()) {
            userInfos.add(userInfoRepository.findById(vid).get());
        }
//        p.setUserInfos(userInfos);
        if (userInfos.size() > 8) {
            List<UserInfo> resultList = new ArrayList<>();
            resultList = userInfos.subList(userInfos.size() - 9, userInfos.size() - 1);
            return resultList;
        }
        return userInfos;
    }

    @Override
    public void visit(String uid,String vid) {
//        首先获取对应的当前用户id
        Personal personal = personalRepository.findByuserId(uid);
        personal.getVisitorIds().add(vid);
        personal.setTodayViewNum(personal.getTodayViewNum()==null?1:personal.getTodayViewNum()+1);
        personalRepository.save(personal);
    }
    @Override
    public List<UserInfo> getAllVisitors(String uid) {
        Personal p = personalRepository.findByuserId(uid);
        System.out.println(p);
        List<UserInfo> userInfos = new ArrayList<>();
        for (String vid : p.getVisitorIds()) {
            userInfos.add(userInfoRepository.findById(vid).get());
        }

        return userInfos;
    }
}
